package com.lahuan.config.datasouce;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.sql.DataSource;

@Import(CustomDatasourceProperties.class)
@Configuration
public class CustomDatasourceConfig {

    @Autowired
    CustomDatasourceProperties props;

    @Bean
    public DataSource dataSource() throws Exception {
        //创建自定义的数据源替代spring数据源
        //spring的数据源在context里有别的数据源时就不会创建
        //所以只要写这样一个bean的配置就可以替代spring的数据源
        return new CustomDataSourcePool(
                props.getDriverClassName(),
                props.getUrl(),
                props.getUsername(),
                props.getPassword(),
                props.getMin(),
                props.getMax(),
                props.getMaxLive()
        );

    }

}
